﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace BasicComponents.tree
{
   public class CheckBoxTreeViewItem : TreeViewItem
    {
        private CheckBox checkBox = new CheckBox();
        Grid grid = new Grid();
        Label label = new Label();
        private Brush foreground = Brushes.Black;
        public new Object Header
        {
            get
            {
                return label.Content;
            }

            set
            {
                label.Content = value;
            }
        }

        public bool IsChecked
        {
            get
            {
                return checkBox.IsChecked.Value;
            }

            set
            {
                checkBox.IsChecked = value;
            }
        }

        public new Brush Foreground
        { 
                get
            {
                    return foreground;
                }

                set
            {
                    foreground = value;
                    this.label.Foreground = foreground;
                }
            
        }

        public CheckBoxTreeViewItem() {
            checkBox.Click -= new RoutedEventHandler(OnCheckChangedHandle);
            checkBox.Click += new RoutedEventHandler(OnCheckChangedHandle);
            ColumnDefinition col1 = new ColumnDefinition();
            ColumnDefinition col2 = new ColumnDefinition();
            RowDefinition row = new RowDefinition();
            col1.Width = GridLength.Auto;
            col2.Width = GridLength.Auto;
            row.Height = GridLength.Auto;
            this.grid.ColumnDefinitions.Add(col1);
            this.grid.ColumnDefinitions.Add(col2);
            this.grid.RowDefinitions.Add(row);
            Grid.SetColumn(this.checkBox,0);
            Grid.SetRow(this.checkBox,0);
            Grid.SetColumn(this.label,1);
            Grid.SetRow(this.label,0);
            this.grid.Children.Add(this.checkBox);
            this.grid.Children.Add(this.label);
            this.checkBox.HorizontalAlignment = HorizontalAlignment.Left;
            this.checkBox.VerticalAlignment = VerticalAlignment.Center;
            this.grid.Margin = new Thickness(0,2,0,0);
            this.checkBox.Margin = new Thickness(0, 0, 0, 0);
            this.checkBox.Padding = new Thickness(0,0,0,0);
            this.label.Margin = new Thickness(2, 0, 0, 0);
            this.label.Padding = new Thickness(0,0,0,0);
            this.Margin = new Thickness(0, 0, 0, 0);
            this.Padding = new Thickness(1,2,2,2);
            base.Header = grid;
            
        }
        #region Add the Click Event  
        //Add a Click Event  
        public static readonly RoutedEvent CheckChangedEvent = EventManager.RegisterRoutedEvent(
            "CheckChanged",RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(CheckBoxTreeViewItem));

        public event RoutedEventHandler CheckChanged
        {
            add { AddHandler(CheckChangedEvent, value); }
            remove { RemoveHandler(CheckChangedEvent, value); }
        }

        protected void RaiseCheckChangedEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(CheckBoxTreeViewItem.CheckChangedEvent);
            RaiseEvent(newEventArgs);
        }

        //objects events handle  
        private void OnCheckChangedHandle(object sender, System.Windows.RoutedEventArgs e)
        {
            RaiseCheckChangedEvent();
        }
        #endregion
         

    }
}
